﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using PhotoGallery.DAL;
using PhotoGallery.Model;
namespace PhotoGallery.DLL
{
    public class AlbumsDataAccess
    {
        albumsDataContext adc = new albumsDataContext();
        /*插入新纪录*/
        public void insert(String uid,String title,String desp) 
        {
            album album = new album();
            album.uid = int.Parse(uid);
            album.title = title;
            album.desp = desp;
            album.visit = 0;
            album.time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            adc.albums.InsertOnSubmit(album);
            adc.SubmitChanges();
        }
        /*删除记录*/
        public void delete(int id)
        {
            var result = from m in adc.albums
                         where m.aid == id
                         select m;
            foreach (album m in result)
                adc.albums.DeleteOnSubmit(m);
            adc.SubmitChanges();
        }
        /*列出全部相册*/
        public List<Albums> getAlltheAlbums() 
        {
            List<Albums> all = new List<Albums>();
            var query = from m in adc.albums
                        select m;
            foreach (album m in query) 
            {
                all.Add(new Albums(m.aid.ToString(), m.uid.ToString(), m.title, m.desp, m.visit, m.time, m.cover.ToString()));
            }
            return all;
        }
        /*根据id查找*/
        public Albums searchAlbumById(string id) 
        {
            int aid = int.Parse(id);
            var query = adc.albums.Where(m => m.aid == aid).Select(m => new Albums(m.aid.ToString(), m.uid.ToString(), m.title, m.desp, m.visit, m.time, m.cover.ToString()));
            foreach (var m in query)
                if (m != null)
                    return m;
                else
                    return null;
            return null;
        }
        /*根据uid查找*/
        public List<Albums> searchAlbumByUid(string id)
        {
            int uid = int.Parse(id);
            List<Albums> list = new List<Albums>();
            var query = adc.albums.Where(m => m.uid == uid).Select(m => new Albums(m.aid.ToString(), m.uid.ToString(), m.title, m.desp, m.visit, m.time,m.cover.ToString()));
            foreach (var m in query)
            {
                if (m != null)
                    list.Add(m);
            }
            return list;
        }
        /*根据uid和title查找*/
        public Albums getAlbumByUidndTitle(string uid, string title) 
        {
            var query = adc.albums.Where(m => m.uid == int.Parse(uid) && m.title == title).Select(m => new Albums(m.aid.ToString(), m.uid.ToString(), m.title, m.desp, m.visit, m.time, m.cover.ToString()));
            foreach (var m in query)
            {
                if (m != null)
                    return m;
                else
                    return null;
            }
            return null;
        }
        /*列出最新创建的相册,参数为取出的相册数目*/
        public List<Albums> searchNewAlbum(string top)
        {
            int choose= int.Parse(top);
            List<Albums> list = new List<Albums>();
            var query = adc.albums.OrderByDescending(m=>m.aid).Select(m => new Albums(m.aid.ToString(), m.uid.ToString(), m.title, m.desp, m.visit, m.time, m.cover.ToString()));
            foreach (var m in query)
            {
                if (m != null)
                    list.Add(m);
                if (list.Count == choose)
                    break;
            }
            return list;
        }
        /*根据封面取得相册*/
        public Albums searchAlbumbyCover(string cover) 
        {
            var query = adc.albums.Where(m => m.cover == int.Parse(cover)).Select(m => new Albums(m.aid.ToString(), m.uid.ToString(), m.title, m.desp, m.visit, m.time, m.cover.ToString()));
            foreach (var m in query)
                if (m != null)
                    return m;
                else
                    return null;
            return null;
        }
        /*修改相册信息*/
        public void modifyAlbum(string id, Albums album)
        {
            var result = from m in adc.albums
                         where m.aid == int.Parse(id)
                         select m;
            foreach (album m in result)
            {
                m.title = album.Title;
                m.desp = album.Desp;
            }
            adc.SubmitChanges();   
        }
        /*修改相册封面*/
        public void modifyAlbum(string id, string cover)
        {
            var result = from m in adc.albums
                         where m.aid == int.Parse(id)
                         select m;
            foreach (album m in result)
            {
                m.cover = int.Parse(cover);
            }
            adc.SubmitChanges();
        }
    }
}
